/**
 * 
 */
var rowNumber;
var pageSize;
var pageNumber;

$(function(){
 	   $('#adstaff')
 		.datagrid(
 				{
 					url : 'admin/queryAdStaffInfo.do?pageSize=10',
 					collapsible : true,
 					width : '80%',
 					height :'450px',
 					iconCls : 'icon-user',
 					fitColumns : true,
 					pagination : true,
 					rownumbers : true,
 					singleSelect:true,
 					checkOnSelect:false,
 					selectOnCheck:false,
 					loadMsg:'正在加载数据',
 					loadFilter:function(data){
 						 for(var i=0;i<data.rows.length;i++){
 							 if(data.rows[i].staffIsOn==0){
 								 data.rows[i].staffIsOn="离职";
 							 }else{
 								 data.rows[i].staffIsOn="在职";
 							 }
 						}  
 						 return data;
 					},
 					onClickRow: function(rowIndex, rowData) {
 						rowNumber=rowIndex;
 						
 					},
 					onDblClickRow :function(rowIndex,rowData){
 						//layer.msg("double click");
 					  },
 					  onLoadSuccess:function(data){
 							$('#adstaff').datagrid('selectRow',rowNumber);
 						},
 					toolbar:'#toolbar',
 					columns : [ [
 	                        /* {
 								field : 'staffId',
 								title : '用户id',
 								width : 50,
 								align : 'center'
 							}, */
 							{
 								field : 'staffAccount',
 								title : '用户名',
 								width : 50,
 								align : 'center'
 							}, 
 							{	
 								hidden:true,
 								field : 'staffPassword',
 								title : '用户密码',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'roleName',
 								title : '身份信息',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'staffName',
 								title : '真实姓名',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'staffTelephone',
 								title : '联系方式',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'staffIsOn',
 								title : '是否在职',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'staffEntryTime',
 								title : '入职时间',
 								width : 50,
 								align : 'center'
 							},
 							{
 								field : 'staffLeaveTime',
 								title : '离职时间',
 								width : 50,
 								align : 'center'
 							}
 						   ] ]
 				});
 	   
 	  //设置分页控件  
 		var p = $('#adstaff').datagrid('getPager');
 		$(p).pagination({
 			pageSize : 10,//每页显示的记录条数，默认为10  
 			pageList : [5,10,20,30,40,50],//可以设置每页记录条数的列表  
 			beforePageText : '第',//页数文本框前显示的汉字  
 			afterPageText : '页    共 {pages} 页',
 			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
 			onSelectPage:function(pageNumber, pageSize){ 
 				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
 				var datas={
        				staffAccount:$("#searchstaffAccount").textbox('getValue'),
        				staffName:$("#searchstaffName").textbox('getValue'),
        				staffIsOn:$("#searchstaffIsOn").combobox('getValue'),
 						pageSize : pageSize
 				}
 				loadUserData(datas);
 		    },
	 		onRefresh:function(pageNumber, pageSize){
	 			var datas={
        				staffAccount:$("#searchstaffAccount").textbox('getValue'),
        				staffName:$("#searchstaffName").textbox('getValue'),
        				staffIsOn:$("#searchstaffIsOn").combobox('getValue'),
 						currentPage : (pageNumber-1)*pageSize,
 						pageSize : pageSize
 				}
 				loadUserData(datas);
	 		}
 		}); 
    });
    	//新增用户
    	function insertAdmin(){
    		
				$("#staffLeaveTime").datetimebox("readonly",true);
				$("#staffAccount").validatebox('readonly',false);
				$('#adstafffm').form('clear');
				$('#adstaffdlg').dialog('open').dialog("setTitle","新增用户");
				$("#saveUrl").val('admin/addAdStaffInfo.do');
    	}
        /* 修改用户 */
        function updateAdmin(){
            var row = $('#adstaff').datagrid('getSelected');
            if (row){
                $('#adstaffdlg').dialog('open').dialog('setTitle','修改');
                $('#adstafffm').form('load',row);
				$('#staffPass').textbox('setValue','***');
                $("#staffLeaveTime").datetimebox("readonly",false);
				$("#staffEntryTime").datetimebox('readonly',true);
				$("#staffAccount").validatebox('readonly',true);
                $("#saveUrl").val('admin/updateAdStaffInfo.do?staffId='+row.staffId);
            }else{
            	layer.msg('请选中一行!');
            }
        }
        /* 保存按钮事件 */
        function adstaffSave(){
        	pageSize = $('#adstaff').datagrid('getPager').data(
    		"pagination").options.pageSize;
    		pageNumber =  $('#adstaff').datagrid('getPager').data(
    		"pagination").options.pageNumber;
    		var datas={
    				currentPage : (pageNumber - 1) * pageSize,
    				pageSize:pageSize
    				};
        	$.ajax({
				url : $("#saveUrl").val(),
				type : "POST",
				dataType : "json",						
				data :$('#adstafffm').serialize(),
				beforeSend: function(){
					var flag=$("#adstafffm").form('enableValidation').form('validate');
					if(flag){
						$('#save').linkbutton({disabled:true}); 
				          return flag;
					}else{
						return flag;
					}
					
		         },
				success : function(result) {
					$('#save').linkbutton({disabled:false}); 
	                  if(result.result==0){
	                	  layer.msg(result.tips);
	                  }else{
	                	  layer.msg(result.tips);
	                	  $('#adstaffdlg').dialog('close');
	                	  $('#adstaff').datagrid('reload',datas);// close the dialog
	                      $('#staffPass').textbox('setValue','***');
	                  }
						
				}
		});  
        }
        /* 删除用户 */
        function deleteAdmin(){
            var row = $('#adstaff').datagrid('getSelected');
            if (row){
            	if(row.staffAccount!='admin'||row.staffAccount!='test'){
                $.messager.confirm('提示','确定要删除吗?',function(r){
                    if (r){
                        $.post('admin/deleteAdStaffInfo.do',{staffId:row.staffId},function(result){
                            if (result.result==1){
                            	layer.msg(result.tips);
                                $('#adstaff').datagrid('reload');    // reload the user data
                            } else {
                            	layer.msg(result.tips);
                            }
                        },'json');
                    }
                });
            }else{
            	layer.msg('禁止删除');
            }
            }else{
            	layer.msg('请选中一行!');
            }
        }
        //分配身份
        function editRole(){
        	var row = $('#adstaff').datagrid('getSelected');
			 if(!!row){
				 if(row.staffAccount!='admin' & row.staffAccount!='test'){
					 if(row.roleName!=null){//如果身份信息不为空,则修改用户
							$("#rolesaveUrl").val("admin/updateRoleStaff.do?staff_id="+row.staffId);
						 }else{
							$("#rolesaveUrl").val("admin/insertRoletoStaff.do");
						 }
						$("#roledlg").dialog('open').dialog('setTitle','分配身份');
						openRole();
			 }else{
				 layer.msg("禁止修改");
				 }
			 }else{
				 layer.msg("请选中一行");
			 }
			 
        }
        //搜索按钮
       	function searchAdStaff(){
       		var pageSize=$('#adstaff').datagrid('getPager').data("pagination").options.pageSize;
       		var datas={
						pageSize : pageSize,
       				staffAccount:$("#searchstaffAccount").textbox('getValue'),
       				staffName:$("#searchstaffName").textbox('getValue'),
       				staffIsOn:$("#searchstaffIsOn").combobox('getValue')
       		}
       		loadUserData(datas);
       	}		
        var loadUserData = function(datas){
   		 $.ajax({
   				url : 'admin/queryAdStaffInfo.do',
   				type : "POST",
   				dataType : "json",						
   				data : datas,
   				success : function(param) {
   						$('#adstaff').datagrid('loadData',param);
   				}
   		});   
   	}
        //重置按钮
	function resetAdStaffButtn(){
		$("#searchstaffAccount").textbox('setValue',''); 
		$("#searchstaffName").textbox('setValue','');
		$("#searchstaffIsOn").combobox('setValue','');
		var pageSize=$('#adstaff').datagrid('getPager').data("pagination").options.pageSize;
		var datas={
				pageSize : pageSize,
		}
		loadUserData(datas);
	}
        //取消按钮
		function adstaffCancle(){
			$('#save').linkbutton({disabled:false}); 
			$('#adstafffm').form('clear');
			$('#adstaffdlg').dialog('close'); 
			$('#staffPass').textbox('setValue','***');
			$("#staffEntryTime").datetimebox('readonly',false);
        }
        
     function openRole(){
    	  $('#role').combobox({    
  		    url:'admin/queryRoleInfo.do',    
  		    valueField:'roleId',    
  		    textField:'roleName',
  		  	loadFilter:function(data){
  		  		try {
  		  		return data.rows;
				} catch (e) {
					//todo
				}
  		  	}
  		});  
     }
     //分配权限的保存按钮
    	function saveRoleBtn(){
    		var row = $('#adstaff').datagrid('getSelected');
    		var role=$("#role").combobox('getValue');
    		 $.ajax({
    				url : $('#rolesaveUrl').val(),
    				type : "POST",
    				dataType : "json",						
    				data : {
    					"staffId":row.staffId,
    					"roleId":role
    				},
    				beforeSend: function(){
  			          return $("#rolefm").form('enableValidation').form('validate');
   		         	},
    				success : function(param) {
    					layer.msg(param.tips);
    					$('#rolefm').form('clear');
    					$('#roledlg').dialog('close'); 
    					 $('#adstaff').datagrid('reload');
    				}
    		});  
    		
    		
    	}  
     //分配权限的取消按钮
    	function cancelRoleBtn(){
    		$('#rolefm').form('clear');
			$('#roledlg').dialog('close'); 
    	}    
    	//查询是否存在相同的名称
		function searchCommon(){
			$.ajax({
   				url : 'admin/queryCommonStaff.do',
   				type : "POST",
   				dataType : "json",						
   				data : {
   					'staffAccount':$('#staffAccount').val(),
   				},
   				success : function(param) {
   					if(param.result==0){
   						layer.msg(param.tips+'请重新输入');
   						$('#save').linkbutton({disabled:true}); 
   					}else{
   						$('#save').linkbutton({disabled:false}); 
   					}
   				}
   		}); 
    	}